package cn.org.timespace.easyframe.util.sms.telecom;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.org.timespace.easyframe.util.SpringBeanFactory;
import cn.org.timespace.easyframe.util.sms.SmsManager;
import cn.org.timespace.easyframe.util.sms.telecom.login.SendLongSms;
import cn.org.timespace.easyframe.util.sms.telecom.receive.ReceiveSms;
import cn.org.timespace.easyframe.util.sms.telecom.smgp.Client;
import cn.org.timespace.easyframe.util.sms.telecom.smgp.bean.Result;
import cn.org.timespace.easyframe.util.sms.telecom.smsUtil.SMGPDeliverMessage;

/**
 * ������� ���Ž���
 * */
public class Receive extends Thread {

	public synchronized static void runStart() {
		try {
			Client client = SendLongSms.login();
			Result result = client.Login();
			Timer timer = new Timer();
			if (result.ErrorCode != 0) {
				timer.schedule(new MyDeliver(), 0, 1000 * 60 * 10);//10����l��һ��
			} else {
				timer.cancel();//l����� �ر� ѭ��l��
				processing(client);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void processing(Client client) {

		List<SMGPDeliverMessage> message_list = ReceiveSms.OnDeliver(client);
		for (SMGPDeliverMessage message : message_list) {
			System.out.println("-----------------");
			System.out.println("Got MsgReport");
			System.out.println("-----------------");
			System.out.println("MsgID       : " + message.getMsgID());
			System.out.println("RecvTime    : " + message.getRecvTime());
			System.out.println("MsgContent  : " + message.getMsgContent());
			System.out.println("ReportMsgID : " + message.getReportMsgID());
			System.out.println("-----------------");

			String[] moblie = getMoblie(message.getSrcTermID());
			// ����Ϣ������ݿ�
			SpringBeanFactory factory = new SpringBeanFactory().getInstance();

			// ������Ϣ
			SmsManager.send(moblie, message.getMsgContent());
		}
	}

	private static class MyDeliver extends TimerTask {
		public void run() {
			new Thread(){
		    	public void run(){
		    		 Receive.runStart();
		    	}
		    }.start();
		}
	}

	/**
	 * ��ȡ�ҳ��绰����
	 * 
	 * @param String
	 *            telphone notNull ��ʦ�绰����
	 * @return String[] ��ʦ��ѧ��ļҳ��绰����
	 * */
	private static String[] getMoblie(String moblie) {
		/*SpringBeanFactory factory = new SpringBeanFactory().getInstance();
		TeacherService teacherService = (TeacherService) factory
				.getBean("statusService");
		List<Parent> parent = teacherService.selectTeachersByTel(moblie);
		String[] strl = null;
		int parentSize = parent.size();
		for (int i = 0; i < parentSize; i++) {
			strl[i] = parent.get(i).getMobilephone();
		}
		return strl;*/
		return null;
	}

}
